package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.formula.enums.RuleFormulaExplainEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.service.FormulaExplainService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formexplain.pojo.ReturnExplainPojo;
import com.xbongbong.pro.formexplain.pojo.dto.FormulaExplainGetDTO;
import com.xbongbong.pro.formexplain.pojo.vo.FormulaExplainGetVO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.ShowTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @version 1.0
 * @date 2020-02-07 14:04
 * @since 1.0
 **/
@Service("formulaExplainService")
public class FormulaExplainServiceImpl extends PaasFormExplainServiceImpl implements FormulaExplainService {

    private static final Logger LOG = LoggerFactory.getLogger(FormulaExplainServiceImpl.class);

    @Resource
    private ProFormHelp proFormHelp;

    @Override
    public FormulaExplainGetVO ruleFormulaList(FormulaExplainGetDTO formulaExplainGetDTO) throws XbbException {
        FormulaExplainGetVO formulaExplainGetVO = new FormulaExplainGetVO();
        try {
            String corpid = formulaExplainGetDTO.getCorpid();
            Long formId = formulaExplainGetDTO.getFormId();
            Integer businessType = formulaExplainGetDTO.getBusinessType();
            // 获取字段信息
            PaasFormExplainEntity paasFormExplainEntity = proFormHelp.getExplainEntity(corpid, formId, businessType);
            List<FieldAttrEntity> explainList = JSON.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
            // 获取适合作为公式的字段
            Iterator<FieldAttrEntity> iterator = explainList.iterator();
            List<ReturnExplainPojo> returnExplainList = new ArrayList<>();
            while (iterator.hasNext()) {
                FieldAttrEntity fieldAttrEntity = iterator.next();
                Integer showType = fieldAttrEntity.getShowType();
                boolean invisible = Objects.nonNull(showType) && (ShowTypeEnum.LIST.getCode() == showType || ShowTypeEnum.DETAIL.getCode() == showType
                        || ShowTypeEnum.LIST_DETAIL.getCode() == showType || ShowTypeEnum.NONE.getCode() == showType);
                if (invisible || Objects.equals(0, fieldAttrEntity.getIsOpen())) {
                    continue;
                }
                boolean isFormulaExplain = RuleFormulaExplainEnum.getFormulaExplain(fieldAttrEntity.getFieldType());
                if (isFormulaExplain) {
                    ReturnExplainPojo returnExplainPojo = new ReturnExplainPojo(fieldAttrEntity.getAttr(), fieldAttrEntity.getAttrName(), fieldAttrEntity.getFieldType(), fieldAttrEntity.getSubForm());
                    SubFormPoJo subFormPoJo = fieldAttrEntity.getSubForm();
                    if (Objects.equals(FieldTypeEnum.SUB_FORM.getType(), fieldAttrEntity.getFieldType())
                            && Objects.nonNull(subFormPoJo.getItems()) && subFormPoJo.getItems().size() != 0) {
                        List<? extends FieldAttrEntity> items = subFormPoJo.getItems();
                        for (int i = 0; i < items.size(); i++) {
                            FieldAttrEntity subField = items.get(i);
                            boolean isFormula = RuleFormulaExplainEnum.getFormulaExplain(subField.getFieldType());
                            if (isFormula) {
                                String attr = fieldAttrEntity.getAttr() + StringConstant.POINT + subField.getAttr();
                                String attrName = fieldAttrEntity.getAttrName() + StringConstant.POINT + subField.getAttrName();
                                Integer fieldType = subField.getFieldType();
                                returnExplainPojo = new ReturnExplainPojo(attr, attrName, fieldType, null);
                                returnExplainList.add(returnExplainPojo);
                            }
                        }
                    } else {
                        returnExplainList.add(returnExplainPojo);
                    }
                }
            }
            formulaExplainGetVO.setFormExplainList(returnExplainList);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("paasFormExplain数据库搜索表单字段后过滤字段出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return formulaExplainGetVO;
    }
}
